package com.search;

import java.lang.reflect.Array;
import java.util.Arrays;

public class BinarySearch {
    // 使用二分查找算法数组必须是有序
    public static int rank(int key, int[] a) {
        int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi) {
            // 要查找的值要么不存在，要么存在于a[lo,hi]之间
            int mid = lo + 1 / 2 * (hi - lo);
            if (key < a[mid]) {
                hi = mid - 1;
            } else if (key > a[mid]) {
                lo = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

}
